package com.space.school.controller;

import com.space.school.domain.Category;
import com.space.school.domain.Product;
import com.space.school.domain.User;
import com.space.school.services.ProductService;
import com.space.school.services.RegisterService;
import com.space.school.services.UserService;
import com.space.school.services.impl.ProductServiceImpl;
import com.space.school.services.impl.RegisterServiceImpl;
import com.space.school.services.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet(name = "productOperateServlet", value = "/productOperateServlet")
public class ProductOperateServlet extends HttpServlet {
    ProductService productService = new ProductServiceImpl();
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String product_id = req.getParameter("product_id");
        String operate = req.getParameter("operate");
        req.getSession().setAttribute("insert_msg", "");
//        id的初始值  因为如果是新增条件的情况
//        userId没有赋值,为null 所以 我初始化id为0
        int id = 0;
        if (product_id != null) {
            id = Integer.parseInt(product_id);
        }
//       删除操作
        if (operate.equals("del")) {
            int num = productService.delete(id);
            if (num > 0) {
//                跳转页面  userServlet doget方法 Redirect 重定向
                resp.sendRedirect("/productInfoByPageServlet");
            }

        } else if (operate.equals("update")) {
//            先把userid 对应的值取出来
            String updateProduct_msg = req.getParameter("updateProduct_msg");
            req.getSession().setAttribute("updateProduct_msg", updateProduct_msg);
            Product product = productService.findById(id);
            if (product != null) {
//                往里面防值
                req.setAttribute("product", product);
                List<Category> categoryList=productService.findAll();
                req.setAttribute("categoryList",categoryList);
                req.getRequestDispatcher("updateProductList.jsp").forward(req, resp);
            }

        } else if (operate.equals("status")) {
            String product_status = req.getParameter("product_status");
            productService.updateStatus(id, product_status);
            resp.sendRedirect("/productInfoByPageServlet");
        } else if (operate.equals("add")) {
            List<Category> categoryList=productService.findAll();
            req.setAttribute("categoryList",categoryList);
            req.getRequestDispatcher("insertProductList.jsp").forward(req, resp);
        }
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        页面提取用户id 和密码
        req.setCharacterEncoding("UTF-8");
        String product_name = req.getParameter("product_name");
        String product_price = req.getParameter("product_price");
        String product_stock = req.getParameter("product_stock");
        String product_description = req.getParameter("product_description");
        String product_icon = req.getParameter("product_icon");
        String product_status = req.getParameter("product_status");
        String category_type = req.getParameter("category_type");
        String product_id = req.getParameter("product_id");

        if (product_id == null) {
//            为新增操作
            boolean insertFlag = productService.isInsert(product_name);
            if (!insertFlag) {
                req.getSession().setAttribute("insert_msg", "该商品已存在,请重新输入");
                req.getRequestDispatcher("insertProductList.jsp").forward(req, resp);
            } else {
                Product product = new Product();
                product.setProduct_name(product_name);
                product.setProduct_price(Double.parseDouble(product_price));
                product.setProduct_stock(Integer.parseInt(product_stock));
                product.setProduct_description(product_description);
                product.setProduct_icon(product_icon);
                product.setProduct_status(Integer.parseInt(product_status));
                product.setCategory_type(Integer.parseInt(category_type));
                productService.save(product);

                resp.sendRedirect("/productInfoByPageServlet");

            }
        } else {
            boolean insertFlag = productService.isInsert(product_name);
            if (!insertFlag) {
                resp.sendRedirect("/productOperateServlet?product_id=" + product_id + "&operate=update"+"&updateProduct_msg="+java.net.URLEncoder.encode("商品名称不能重复", "UTF-8"));

            }else {
                Map<String,String[]> map=req.getParameterMap();
                productService.update(map);
                resp.sendRedirect("/productInfoByPageServlet");
            }
        }
    }
}

